import { Inject, Post, Body, Get, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { AppointmentInfoEntity } from '../../entity/info';
import { AppointmentInfoService } from '../../service/info';
import { AppointmentBoardService } from '../../service/board';
import { DoctorInfoEntity } from '../../../doctor/entity/info';
import { StudentStudentEntity } from '../../../student/entity/student';

/**
 * 预约信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: AppointmentInfoEntity,
  service: AppointmentInfoService,
  pageQueryOp: {
    fieldEq: [
      'a.userId', 
      'a.doctorId', 
      'appointmentStatus', 
      'a.appointmentType', 
      'a.comeType',
      'a.shopId', 
      'c.sales', 
      'c.project', 
      'c.source', 
      'c.inGroup', 
      'c.birthday', 
      'c.remark',
    ],
    fieldLike: [],
    select: ['a.*', 
      'b.name as doctorName', 
      'c.name as userName', 
      'c.phone as userPhone', 
      'c.source as userSource',
      'c.sales as userSales',
      'c.project as userProject',
      'c.inGroup as userInGroup',
      'c.birthday as userBirthday',
      'c.remark as userRemark',
      'c.gender as userGender',
    ],
    join: [
      {
        entity: DoctorInfoEntity,
        alias: 'b',
        condition: 'a.doctorId = b.id',
        type: 'leftJoin',
      },
      {
        entity: StudentStudentEntity,
        alias: 'c',
        condition: 'a.studentId = c.id',
        type: 'leftJoin',
      },
    ],
    where: async ctx => {
      const { appointmentStatus, source, inGroup, project, appointmentTimeStartTime, appointmentTimeEndTime, phone, name } = ctx.request.body;
      const where = [];
      // if (appointmentStatus && appointmentStatus.length > 0) {
      //   where.push([
      //     'a.appointmentStatus IN (:...appointmentStatus)',
      //     { appointmentStatus },
      //   ]);
      // }
      // if (startTime) {
      //   where.push(['a.appointmentTime >= :startTime', { startTime }]);
      // }
      // if (endTime) {
      //   where.push(['a.appointmentTime <= :endTime', { endTime }]);
      // }
      if (appointmentTimeStartTime && appointmentTimeEndTime) {
        where.push([
          'a.appointmentTime BETWEEN :startTime AND :endTime',
          { 
            startTime: appointmentTimeStartTime,
            endTime: appointmentTimeEndTime 
          }
        ]);
      }
      
      // if (appointmentTime) {
      //   where.push([
      //     "DATE(a.appointmentTime) = :appointmentDate",
      //     { appointmentDate: appointmentTime }
      //   ]);
      //   console.log('查询日期:', appointmentTime);
      // }

      if (source) {
        where.push([
          'c.source = :source',
          { source }
        ]);
      }

      if (inGroup) {
        where.push([
          'c.inGroup = :inGroup',
          { inGroup }
        ]);
      }

      if (project) {
        where.push([
          'c.project = :project',
          { project }
        ]);
      }

      if (phone) {
        where.push([
          'c.phone = :phone',
          { phone }
        ]);
      }

      if (name) {
        where.push([
          'c.name = :name',
          { name }
        ]);
      }
      
      return where;
    },
  },
})
export class AdminAppointmentInfoController extends BaseController {
  @Inject()
  appointmentInfoService: AppointmentInfoService;

  @Inject()
  appointmentBoardService: AppointmentBoardService;

  @Post('/appoint', { summary: '预约' })
  async appoint(
    @Body() param: {
      studentId: number;
      doctorId: number;
      shopId: number;
      date: string;
      time: string;
    }
  ) {
    const result = await this.appointmentInfoService.appoint(param);
    return this.ok(result);
  }

  // 取消预约
  @Post('/cancelAppoint', { summary: '取消预约' })
  async cancelAppoint(
    @Body() param: {
      appointId: number;
    }
  ) {
    const result = await this.appointmentInfoService.appointCancle(param);
    return this.ok(result);
  }

  @Get('/appointSalesBoard', { summary: '预约行为看板' })
  async appointSalesBoard(
    @Query() param: {
      startTime: string;
      endTime: string;
    }
  ) {
    const result = await this.appointmentInfoService.appointSalesBoard(param);
    return this.ok(result);
  }

  @Get('/board', { summary: '预约看板' })
  async board(
    @Query() param: {
      visitType: string;
      startTime: string;
      endTime: string;
    }
  ) {
    const result = await this.appointmentInfoService.board(param);
    return this.ok(result);
  }

  @Get('/appointBoard', { summary: '预约看板' })
  async appointBoard(
    @Query() param: {
      shopId: number;
      channelId: number;
    }
  ) {
    const result = await this.appointmentInfoService.appointBoard(param);
    return this.ok(result);
  }

  // shopConvertPerformance
  @Get('/shopConvertPerformance', { summary: '门店绩效' })
  async shopConvertPerformance(
    @Query() param: {
      shopId: number;
      channelId: number;
    }
  ) {
    const result = await this.appointmentInfoService.shopConvertPerformance(param);
    return this.ok(result);
  }

  @Get('/getSalesAppointConvertBoard', { summary: '查看预约' })
  async getSalesAppointConvertBoard(
    @Query() param: {
      startTime?: string; // 开始时间，可选
      endTime?: string;   // 结束时间，可选
    }
  ) {
    const result = await this.appointmentBoardService.getSalesAppointConvertBoard(param);
    return this.ok(result);
  }

  @Get('/getSalesAppointConvertBoardForDaren', { summary: '达人预约数据' })
  async getSalesAppointConvertBoardForDaren(
    @Query() param: {
      startTime?: string; // 开始时间，可选
      endTime?: string;   // 结束时间，可选
    }
  ) {
    const result = await this.appointmentBoardService.getSalesAppointConvertBoardForDaren(param);
    return this.ok(result);
  }

  @Get('/getSalesAppointConvertBoardForDarenAction', { summary: '达人预约数据' })
  async getSalesAppointConvertBoardForDarenAction(
    @Query() param: {
      startTime?: string; // 开始时间，可选
      endTime?: string;   // 结束时间，可选
    }
  ) {
    const result = await this.appointmentBoardService.getSalesAppointConvertBoardForDarenAction(param);
    return this.ok(result);
  }

  @Get('/dateAppoint', { summary: '查看预约' })
  async dateAppoint(
    @Query() param: {
      doctorId: number;
      shopId: number;
      date: string;
      endDate?: string
    }
  ) {
    // console.log('dateAppoint', param);
    const result = await this.appointmentInfoService.dateAppoint(param);
    return this.ok(result);
  }

  @Get('/dateClass', { summary: '查看班级' })
  async dateClass(
    @Query() param: {
      doctorId: number;
      shopId: number;
      date: string;
      endDate?: string
    }
  ) {
    const result = await this.appointmentInfoService.dateClass(param);
    return this.ok(result);
  }
  
  @Post('/appointCome', { summary: '预约到店' })
  async appointCome(@Body() param: { appointId: number }) {
    const result = await this.appointmentInfoService.appointCome(param);
    return this.ok(result);
  }

  // 取消到店
  @Post('/appointCancelCome', { summary: '取消到店' })
  async appointCancelCome(@Body() param: { appointId: number }) {
    const result = await this.appointmentInfoService.appointCancelCome(param);
    return this.ok(result);
  }

  // 分页2  page2(query, options)
  // const { body } = this.baseCtx.request;
  // return this.ok(await this.service.page(body, this.curdOption.pageQueryOp, this.connectionName));
  @Get('/page2', { summary: '分页2' })
  async page2(
    @Query() query: {}, options: {}
  ) {
    const result = await this.appointmentInfoService.page2(query, options);
    return this.ok(result);
  }

  // 限制门店星期的预约人数
  @Post('/updateShopWeekLimit', { summary: '限制门店星期的预约人数' })
  async updateShopWeekLimit(
    @Body() param: {
      shopId: number;
      week: number;
      limit: number;
    }
  ) {
    const result = await this.appointmentInfoService.updateShopWeekLimit(param);
    return this.ok(result);
  }

  // 获取门店限制的星期数组
  @Get('/getShopWeekLimit', { summary: '获取门店限制的星期数组' })
  async getShopWeekLimit(
    @Query() param: {
      shopId: number;
    }
  ) {
    const result = await this.appointmentInfoService.getShopWeekLimit(param.shopId);
    return this.ok(result);
  }

  // 获取门店限制的日期数组
  @Get('/getShopDateLimit', { summary: '获取门店限制的日期数组' })
  async getShopDateLimit(
    @Query() param: {
      shopId: number;
      week?: number;
      date?: string;
      type?: number;
    }
  ) {
    const result = await this.appointmentInfoService.getShopDateLimit(param.shopId, param.week, param.date, param.type);
    return this.ok(result);
  }
}
